﻿$(function () {
    window.progressDialog = new ProgressDialog();
    window.infoDialog = new InfoDialog();
    window.ajaxClass = new AjaxClass();
    window.resizeFlexiGrid = new ResizeFlexiGrid(".flexigrid", "#List1", ".flexigrid .bDiv", ["#TitleTR", "#ButtonTR", ".flexigrid .hDiv"], 4, 4);

    $("#List1").flexigrid({
        colModel: [
                { display: '自动编号', name: 'AutoID', width: 50, sortable: false, align: 'left', hide: true },
                { display: '一级菜单', name: 'Locatin', width: 100, sortable: false, align: 'left', hide: false },
                { display: '二级菜单', name: 'Locatin', width: 100, sortable: false, align: 'left', hide: false },
                { display: '三级菜单', name: 'Locatin', width: 100, sortable: false, align: 'left', hide: false },
                { display: '权限设置', name: 'Locatin', width: 600, sortable: false, align: 'left', hide: false }
                ],

        dataType: 'json',
        width: "auto",
        height: "auto",
        resizable: false,
        showToggleBtn: false,
        striped: false,

        nonSelect: true,
        singleSelect: true,

        procmsg: '正在处理数据，请稍候……',
        nomsg: '没有符合条件的记录存在。',
        errormsg: '处理数据时出错，请与系统管理员联系！',

        onError: FlexiGrid_Error
    });
    $(".flexigrid .hDiv").bind("selectstart", function () { return false; });
    window.resizeFlexiGrid.Width();

    /* 声明各种事件 --------------------------------------------------------------------------------------------*/

    $("#ButtonSave").button({ icons: { primary: "ui-icon-disk"} }).click(function () {
        var TempJson = new Array();
        var ActionOperations = new Array();
        var ActionOperation = { "OperationKey": "清空菜单权限", "Parameters": null };
        ActionOperations.push(ActionOperation);
        $("#List1 tr").each(function (index, element) {
            if ($(element).find("input[type='checkbox']:eq(0)").is(":checked")) {
                ActionOperation = { "OperationKey": "插入菜单权限", "Parameters": { "@MenuID": $(element).find("div:eq(0)").html(), "@Permissions": ""} };
                $(element).find("input[type='checkbox']:gt(0)").each(function (index, element) {
                    if ($(element).is(":checked")) {
                        if (ActionOperation.Parameters["@Permissions"] == "") {
                            ActionOperation.Parameters["@Permissions"] = $(element).val();
                        } else {
                            ActionOperation.Parameters["@Permissions"] += "," + $(element).val();
                        }
                    }
                });
                ActionOperations.push(ActionOperation);
            }
        });
        TempJson.push(ActionOperations);
        var JsonSend = { "ActionParameters": JSON.stringify(TempJson) };
        window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "window.location.href = JsonOriginal.UrlReferrer;", "", true, false);
    });

    $("#ButtonBack").button({ icons: { primary: "ui-icon-arrowreturnthick-1-w"} }).click(function () {
        window.location.href = JsonOriginal.UrlReferrer;
    });
});

/* 列表事件 --------------------------------------------------------------------------------------------*/

function FlexiGrid_Error(XMLHttpRequest, textStatus, errorThrown) {
    infoDialog.OpenDialog("处理数据时出错，请与系统管理员联系！", "Error");
}  

/* 功能方法 --------------------------------------------------------------------------------------------*/

function SetListData(ListID, ListName) {
    window.resizeFlexiGrid.MinHeight();
    var CheckList, HavePermissions, Permissions, Checked, Disabled, Style;
    var List_Data = { "page": 1, "total": 1, "rows": new Array() };
    var ListObj = JsonOriginal.Operations[ListName].OperationData;
    if (ListObj != null) {
        for (var i = 0; i < ListObj.length; i++) {
            Checked = "";
            if (ListObj[i]["HavePermissions"] != null) {
                Checked = "checked=\"checked\"";
            }
            CheckList = "<input onclick=\"if(!$(this).is(':checked')){$(this).parent().find('input:gt(0)').attr('disabled','disabled');$(this).parent().find('label:gt(0)').css('color','#b1b1b1');}else{$(this).parent().find('input:gt(0)').removeAttr('disabled');$(this).parent().find('label:gt(0)').removeAttr('style');}\" class=\"CheckBox\" type=\"checkbox\" id=\"" + ListObj[i]["AutoID"] + "_进入\" value=\"进入\" name=\"进入\" " + Checked + "/><label for=\"" + ListObj[i]["AutoID"] + "_进入\">进入</label>"; ;
            if (ListObj[i]["Permissions"] != "") {
                Permissions = ListObj[i]["Permissions"].split(",");
                for (var j = 0; j < Permissions.length; j++) {
                    Checked = "", Disabled = "", Style = "";
                    if (ListObj[i]["HavePermissions"] != null) {
                        if ((ListObj[i]["HavePermissions"] + ",").indexOf(Permissions[j] + ",") > -1) {
                            Checked = "checked=\"checked\"";
                        }
                    } else {
                        Disabled = " disabled=\"disabled\"";
                        Style = " style=\"color:#b1b1b1\"";
                    }
                    CheckList += "<input class=\"CheckBox\" type=\"checkbox\" id=\"" + Permissions[j] + "\" value=\"" + Permissions[j] + "\" name=\"" + Permissions[j] + "\" " + Checked + Disabled + "/><label for=\"" + Permissions[j] + "\"" + Style + ">" + Permissions[j] + "</label>";
                }
            }
            List_Data.rows.push({ "cell": [ListObj[i]["AutoID"], ListObj[i]["FirstName"], ListObj[i]["SecondName"], ListObj[i]["ThirdName"], CheckList] });
        }
    }
    $("#" + ListID).flexAddData(List_Data);
    $("#" + ListID + " tr td div").each(function (index, element) { if ($(element).find("*").size() == 0) { $(element).attr("title", $(element).html() == "&nbsp;" ? "" : $(element).text()) } });
    window.resizeFlexiGrid.Height();
}

/* 初始化 --------------------------------------------------------------------------------------------*/

$(function () {
    SetListData("List1", "菜单权限列表");
    var Rows = document.getElementById("List1").rows;
    var Rowspan = 1;
    if (Rows.length >= 2) {
        for (var j = Rows[0].cells.length - 3; j > 0; j--) {
            for (var i = Rows.length - 1; i >= 0; i--) {
                if (i > 0 && $(Rows[i].cells[j]).find("div").html() == $(Rows[i - 1].cells[j]).find("div").html()) {
                    $(Rows[i].cells[j]).remove();
                    Rowspan++;
                } else {
                    $(Rows[i].cells[j]).attr("rowspan", Rowspan);
                    Rowspan = 1;
                }
            }
        }
    }
    window.resizeFlexiGrid.Resize("resizeFlexiGrid");
});